# encoding: utf-8
"""
Database models
--------------------
"""
from sqlalchemy import ForeignKey
from sqlalchemy_utils import Timestamp

from app.extensions import db

from app.modules.vcloud.models import JsonEncoder
from app.modules.zones.models import Zone


class App(db.Model, Timestamp, JsonEncoder):
    """ database model. """
    app_id = db.Column(db.String(64), primary_key=True)
    zone_code = db.Column(db.String(32), ForeignKey('zone.zone_code'), nullable=True)
    app_name = db.Column(db.String(32), nullable=True)
    port = db.Column(db.Integer, nullable=True)
    app_type = db.Column(db.String(32), nullable=True)
    db_name = db.Column(db.String(32), nullable=True)
    db_type = db.Column(db.String(16), nullable=True)
    db_status = db.Column(db.String(16), nullable=True)
    on_beetle = db.Column(db.String(10), nullable=True)
    status = db.Column(db.String(16), nullable=True)
    version = db.Column(db.String(64), nullable=True)
    mem_level = db.Column(db.Integer, nullable=True)
    zone = db.relationship(Zone)
    last_msg = db.Column(db.Text(1024), nullable=True)
    # db_version = db.Column(db.String(32), nullable=True)
    app_package_create_date = db.Column(db.String(32), nullable=True)
    db_dump_create_date = db.Column(db.String(32), nullable=True)
    app_install_date = db.Column(db.DateTime, nullable=True)
    app_start_date = db.Column(db.DateTime, nullable=True)
    app_stop_date = db.Column(db.DateTime, nullable=True)
    db_import_date = db.Column(db.DateTime, nullable=True)
    disabled = db.Column(db.Boolean, nullable=False, default=False)
    """
    starting_order
    基础组件应用为1-100
    基础应用为101-200
    普通应为为201--
    """
    starting_order = db.Column(db.Integer, nullable=False, default=99999)

    is_running = True
    running_pipeline = None

    def __init__(self, app_id, zone_code=None, app_name=None, port=None,
                 app_type=None, db_name=None, db_type=None, status=None, version=None, on_beetle=None, db_status=None,
                 is_running=True, db_version=None, last_msg=None, mem_level=1, app_install_date=None,
                 db_dump_create_date=None, app_package_create_date=None,app_start_date=None,
                 db_import_date=None, running_pipeline=None, disabled=False, starting_order=99999,app_stop_date=None):
        self.app_id = app_id
        self.zone_code = zone_code
        self.app_name = app_name
        self.port = port
        self.app_type = app_type
        self.db_name = db_name
        self.db_type = db_type
        self.status = status
        self.on_beetle = on_beetle
        self.db_status = db_status
        self.version = version
        self.is_running = is_running
        self.mem_level = mem_level
        self.last_msg = last_msg
        self.db_version = db_version
        self.app_install_date = app_install_date
        self.db_import_date = db_import_date
        self.app_package_create_date = app_package_create_date
        self.db_dump_create_date = db_dump_create_date
        self.disabled = disabled
        self.starting_order = starting_order
        self.running_pipeline = running_pipeline
        self.app_start_date = app_start_date
        self.app_stop_date = app_stop_date

    def __repr__(self):
        return (
            "<{class_name}("
            "app_id={self.app_id}, "
            "zone_code=\"{self.zone_code}\", "
            "app_name=\"{self.app_name}\", "
            "port=\"{self.port}\", "
            "app_type=\"{self.app_type}\", "
            "db_name=\"{self.db_name}\", "
            "db_type=\"{self.db_type}\", "
            "status=\"{self.status}\", "
            "on_beetle=\"{self.on_beetle}\", "
            "db_status=\"{self.db_status}\", "
            "mem_level=\"{self.mem_level}\", "
            "version=\"{self.version}\", "
            "is_running=\"{self.is_running}\", "
            "last_msg=\"{self.last_msg}\", "
            "app_install_date=\"{self.app_install_date}\", "
            "db_import_date=\"{self.db_import_date}\", "
            "app_package_create_date=\"{self.app_package_create_date}\", "
            "db_dump_create_date=\"{self.db_dump_create_date}\", "
            "disabled=\"{self.disabled}\", "
            "starting_order=\"{self.starting_order}\", "
            "running_pipeline=\"{self.running_pipeline}\", "
            "app_stop_date=\"{self.app_stop_date}\", "
            "app_start_date=\"{self.app_start_date}\", "
            ")>".format(
                class_name=self.__class__.__name__,
                self=self
            )
        )

    def to_dict(self):
        return {
            'app_id': self.to_str(self.app_id),
            'zone_code': self.to_str(self.zone_code),
            'app_name': self.to_str(self.app_name),
            'port': self.to_str(self.port),
            'app_type': self.to_str(self.app_type),
            'db_name': self.to_str(self.db_name),
            'db_type': self.to_str(self.db_type),
            'status': self.to_str(self.status),
            'on_beetle': self.to_str(self.on_beetle),
            'db_status': self.to_str(self.db_status),
            'mem_level': self.to_str(self.mem_level),
            'version': self.to_str(self.version),
            'is_running': self.to_str(self.is_running),
            'last_msg': self.to_str(self.last_msg),
            'app_install_date': self.app_install_date.strftime('%Y-%m-%d %H:%M:%S') if self.app_install_date else None,
            'app_start_date': self.app_start_date.strftime('%Y-%m-%d %H:%M:%S') if self.app_start_date else None,
            'app_stop_date': self.app_stop_date.strftime('%Y-%m-%d %H:%M:%S') if self.app_stop_date else None,
            'db_import_date': self.db_import_date.strftime('%Y-%m-%d %H:%M:%S') if self.db_import_date else None,
            'app_package_create_date': self.to_str(self.app_package_create_date),
            'db_dump_create_date': self.to_str(self.db_dump_create_date),
            'disabled': self.to_str(self.disabled),
            'starting_order': self.to_str(self.starting_order),
            'running_pipeline': self.to_str(self.running_pipeline),
        }

    @staticmethod
    def to_str(value):
        # if value is not None and not value == '':
        #     return str(value)
        # else:
        return value
